// addrManage.js
var app = getApp()
Page({

  data: {
  
  },
  //地址删除点击按钮
  deleteClick: function (e) {
    var id = e.currentTarget.dataset.id;
    var thiz = this

    thiz.data.addrData.forEach(function (element, index) {
      if (element.address_id == id) {
        wx.showModal({
          title: '提示',
          content: '是否删除此地址',
          success: function (res) {
            if (res.confirm) {
              thiz.setData({
                hiddenLoading: false
              })
              var domain = app.globalData.domain
              var addrDelUrl = app.globalData.addrDelInterface
              var cid = app.globalData.cid
              var kui_key = app.globalData.kui_key
              var addrDel = domain + addrDelUrl;

              var addrListUrl = app.globalData.addrInfoInterface
              var addrList = domain + addrListUrl;
              var user_id = app.globalData.inv_i

              wx.request({
                url: addrDel,
                method: 'POST',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                data: {
                  cid: cid,
                  kui_key: kui_key,
                  id: element.address_id,
                  model: "MINI",
                  user_id: user_id
                },
                success: function (res) {

                  wx.request({
                    url: addrList,
                    method: 'POST',
                    header: { 'content-type': 'application/x-www-form-urlencoded' },
                    data: {
                      cid: cid,
                      kui_key: kui_key,
                      model: "MINI",
                      user_id: user_id
                    },
                    success: function (res) {

                      if (res.data.length > 0) {

                        thiz.setData({
                          addrData: res.data,
                          hiddenLoading: true
                        })
                      } else {
                        thiz.setData({
                          addrData: [],
                          hiddenLoading: true
                        })
                      }
                    }
                  })
                }
              })
            }
          }
        })
      }
    })

  },
  //添加地址
  addAddrClick: function () {
    var thiz = this
    wx.redirectTo({
      
      url: '../addrAditor/addrAditor?type=2' + '&postGoods=' + thiz.data.postGoods + '&pay_goods=' + thiz.data.pay_goods + '&totalPrice=' + thiz.data.totalPrice,
    })
  },
  //地址编辑点击按钮
  adatorClick: function (e) {
    
    var id = e.currentTarget.dataset.id;
    var thiz = this

    thiz.data.addrData.forEach(function (element, index) {
      if (element.address_id == id) {
        
        wx.redirectTo({
          url: '../addrAditor/addrAditor?url=addrSelect&address_id=' + element.address_id + '&postGoods=' + thiz.data.postGoods + '&pay_goods=' + thiz.data.pay_goods + '&totalPrice=' + thiz.data.totalPrice,
        })
      }
    })
  },
  //地址选择
  addrSelectClick : function(e){
    var thiz = this
    var id = e.currentTarget.dataset.id
    wx.redirectTo({
//      url: '../cartConfirm/cartConfirm?address_id=' + id + '&postGoods=' + thiz.data.postGoods + '&totalPrice=' + thiz.data.totalPrice,
      url: '../cartConfirm/cartConfirm?address_id=' + id + '&postGoods=' + thiz.data.postGoods + '&pay_goods=' + thiz.data.pay_goods + '&totalPrice=' + thiz.data.totalPrice,
    })


  },
  onLoad: function (options) {
    var thiz = this
    var id = null;
    if(options.address_id){
      id = options.address_id
    }

    var background_color = app.globalData.background_color

    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: background_color,
    })

    if (options.pay_goods && options.totalPrice && options.postGoods) {
      thiz.setData({
        postGoods: options.postGoods,
        pay_goods: options.pay_goods,
        totalPrice : options.totalPrice
      })
    }
    var domain = app.globalData.domain
    var addrListUrl = app.globalData.addrInfoInterface
    var cid = app.globalData.cid
    var kui_key = app.globalData.kui_key

    var addrList = domain + addrListUrl;
    var user_id = app.globalData.inv_i

    wx.request({
      url: addrList,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        cid: cid,
        kui_key: kui_key,
        model: "MINI",
        user_id: user_id
      },
      success: function (res) {

        if (res.data.length > 0) {

          thiz.setData({
            addrData: res.data,
            hiddenLoading: true
          })
        } else {
          thiz.setData({
            addrData: [],
            hiddenLoading: true
          })
        }
      }
    })
    
    //获取系统信息
    var thiz = this;

    wx.getSystemInfo({
      success: function (res) {
        thiz.setData({
          hiddenLoading : false,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
        });
      }
    })
  },
  onReachBottom: function(){

  }
})